package composite;

public class NotEmpty<T extends Comparable<T>> implements Liste<T>
{
	private T head;
	private Liste<T> tail;
	
	public NotEmpty(T element, Liste<T> tail)
	{
		head = element;
		this.tail = tail;
	}
	
	public NotEmpty(T element)
	{
		head = element;
		this.tail = new Empty<T>();
	}
	
	@Override
	public T head() {
		return head;
	}
	
	@Override
	public Liste<T> tail() {
		return tail;
	}
	
	@Override
	public boolean isEmpty()
	{
		return false;
	}
	
	@Override
	public int length()
	{
		return tail.length() + 1;
	}

	@Override
	public Liste<T> putLast(T element)
	{
		return new NotEmpty<T>(head, tail.putLast(element));
	}
	
	@Override
	public String toString()
	{
		return " " +head + tail.toString();
	}
	
	@Override
	public Liste<T> reverse()
	{
		return tail.reverse().putLast(head);
	}

	@Override
	public Liste<T> insert(T element)
	{
		if(element.compareTo(head) == 0)
		{
			return this;
		}
		else if(element.compareTo(head) > 0)
		{
			return new NotEmpty<T>(head, tail.insert(element));
		}
		else
		{
			return new NotEmpty<T>(element, this);
		}
	}

	@Override
	public Liste<T> sort()
	{
		return tail.sort().insert(head);
	}
}
